package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.TbLmtrepairWorkdetail;
import com.ruoyi.tb.lmt.domain.TbLmttype;
import com.ruoyi.tb.lmt.domain.TbRepairplanlist;
import com.ruoyi.tb.lmt.domain.bo.TbRepairplanlistBo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbWorktemplateBo;
import com.ruoyi.tb.lmt.domain.vo.TbWorktemplateVo;
import com.ruoyi.tb.lmt.domain.TbWorktemplate;
import com.ruoyi.tb.lmt.mapper.TbWorktemplateMapper;
import com.ruoyi.tb.lmt.service.ITbWorktemplateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 年修/定检内容Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@RequiredArgsConstructor
@Service
public class TbWorktemplateServiceImpl implements ITbWorktemplateService {

    private final TbWorktemplateMapper baseMapper;

    @Autowired
    private TbWorktemplateMapper tbWorktemplateMapper;
    /**
     * 查询年修/定检内容
     */
    @Override
    public TbWorktemplateVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询年修/定检内容列表
     */
    @Override
    public TableDataInfo<TbWorktemplateVo> queryPageList(TbWorktemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbWorktemplate> lqw = buildQueryWrapper(bo);
        // 在这里添加根据 CreateTime 降序排序
        pageQuery.setOrderByColumn("updateTime");
        pageQuery.setIsAsc("desc");
/*        lqw.select(TbWorktemplate::getID,TbWorktemplate::getLmtType,
            TbWorktemplate::getRepairType,TbWorktemplate::getWorkKind,TbWorktemplate::getWorkCategory,
            TbWorktemplate::getWorkCategoryId,TbWorktemplate::getWorkName,TbWorktemplate::getWorkMode,
            TbWorktemplate::getOrderNo,TbWorktemplate::getCreateTime,TbWorktemplate::getUpdateTime,
            TbWorktemplate::getWorkRequire,TbWorktemplate::getWorkHours,TbWorktemplate::getWorkGroup1,
            TbWorktemplate::getWorkGroup2,TbWorktemplate::getWorkUser1,TbWorktemplate::getWorkUser2,
            TbWorktemplate::getWorkGroup3,TbWorktemplate::getWorkUser3,TbWorktemplate::getRemark,
            TbWorktemplate::getWorkDaySeq,TbWorktemplate::getWorkDirectName,
            TbWorktemplate::getWorkDirectUrl
            );*/
        Page<TbWorktemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询年修/定检内容列表
     */
    @Override
    public List<TbWorktemplateVo> queryList(TbWorktemplateBo bo) {
        LambdaQueryWrapper<TbWorktemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbWorktemplate> buildQueryWrapper(TbWorktemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbWorktemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLmtType()), TbWorktemplate::getLmtType, bo.getLmtType());
        lqw.like(StringUtils.isNotBlank(bo.getRepairType()), TbWorktemplate::getRepairType, bo.getRepairType());
        lqw.like(StringUtils.isNotBlank(bo.getRepairTimes()), TbWorktemplate::getRepairTimes, bo.getRepairTimes());
        lqw.like(StringUtils.isNotBlank(bo.getWorkKind()), TbWorktemplate::getWorkKind, bo.getWorkKind());
        lqw.like(StringUtils.isNotBlank(bo.getWorkCategory()), TbWorktemplate::getWorkCategory, bo.getWorkCategory());
        lqw.like(StringUtils.isNotBlank(bo.getWorkName()), TbWorktemplate::getWorkName, bo.getWorkName());
        if (bo.isFormConfigNotNull()) {
            lqw.isNotNull(TbWorktemplate::getFormConfig);
        }
        lqw.orderByAsc(TbWorktemplate::getOrderNo);
        return lqw;
    }

    /**
     * 新增年修/定检内容
     */
    @Override
    public Boolean insertByBo(TbWorktemplateBo bo) {
        TbWorktemplate add = BeanUtil.toBean(bo, TbWorktemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改年修/定检内容
     */
    @Override
    public Boolean updateByBo(TbWorktemplateBo bo) {
        TbWorktemplate update = BeanUtil.toBean(bo, TbWorktemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbWorktemplate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除年修/定检内容
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TbWorktemplateVo> selectListByTbRepairplanlistBo(TbRepairplanlistBo bo) {
        return tbWorktemplateMapper.selectListByTbRepairplanlistBo(bo);
    }

    @Override
    public String formConfig(Long id) {
        return tbWorktemplateMapper.selectFormConfig(id);
    }

    @Override
    public boolean existsById(Long id) {
        return tbWorktemplateMapper.existsById(id);
    }


}
